//
//  CarInfoViewController.m
//  Logistics
//
//  Created by hy002 on 2018/1/16.
//  Copyright © 2018年 HUYU. All rights reserved.
//

#import "CarInfoViewController.h"
#import "TruckTypeModel.h"
#import "LengthModel.h"

static NSString *cellPath = @"CarInfoCell";
@interface CarInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *allTagAry;
@property (nonatomic, strong)NSArray *allTypeAry;



@end

@implementation CarInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self requestTruckType];
    
}

- (void)creatUI
{
    self.navigationItem.title = @"信息筛选";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-65*displaywidth-SCREER_HeiX) style:UITableViewStylePlain];
    _tableview.backgroundColor = [UIColor whiteColor];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    UIButton *soureBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX-55*displaywidth, SCREEN_WIDTH-30*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(soureBtnAction) Title:@"确认" Font:16.0];
    soureBtn.backgroundColor = RGB(255, 153, 0);
    [soureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    soureBtn.layer.cornerRadius = 5;
    soureBtn.layer.masksToBounds = YES;
    [self.view addSubview:soureBtn];
}

- (void)soureBtnAction
{
    NSString *truckID = @"";
    NSString *lenthStr = @"";
    NSString *baffID = @"";
    if (_allTypeAry.count > 0)
    {
        TruckTypeModel *model = _allTypeAry[_TruckType];
        truckID = [NSString stringWithFormat:@"%@,%@",model.name,model.ID];
        NSArray *lenthAry = model.truckLengths;
        if (lenthAry.count > 0)
        {
            LengthModel *lenModel = lenthAry[_lenthType];
            lenthStr = s_str(lenModel.length);
        }
    }
    baffID = @"1";
    if (_baffType == 1)
    {
        baffID = @"0";
    }
    
    
    if (self.closeBlock != nil)
    {
        self.closeBlock(truckID, lenthStr,baffID,_TruckType,_lenthType,_baffType);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    NSArray *titNameAry = @[@"",@"车型",@"车长",@"尾板选择"];
    if (indexPath.section == 0)
    {
        return cell;
    }
    if (indexPath.section != 0)
    {
        UILabel *nameLabel = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 15*displaywidth, SCREEN_WIDTH-30*displaywidth, 20*displaywidth) Font:15.0 Text:titNameAry[indexPath.section] Color:BlaTitColor];
        [cell.contentView addSubview:nameLabel];
    }
    NSArray *general;
    if (indexPath.section == 0)
    {
        general = _allTagAry;
    }
    else if (indexPath.section == 1)
    {
        general = _allTypeAry;
        
    }
    else if (indexPath.section == 2)
    {
        if (_TruckType != 0)
        {
            TruckTypeModel *model = _allTypeAry[_TruckType];
            general = model.truckLengths;
        }
    }
    else
    {
        general = @[@"有尾板",@"无尾板"];
    }
    
    CGFloat W = (SCREEN_WIDTH-70*displaywidth)/4;
    NSInteger num = general.count;
    NSArray *typeIdAry = @[@"",s_Integer(_TruckType),s_Integer(_lenthType),s_Integer(_baffType)];
    CGFloat Height = 50*displaywidth;
    if (indexPath.section == 0)
    {
        Height = 15*displaywidth;
    }
    for (int i = 0; i < num; i++)
    {
        NSString *name = @"";
        if (indexPath.section == 0 || indexPath.section == 1)
        {
            TruckTypeModel *model = general[i];
            name = model.name;
        }
        else if (indexPath.section == 2)
        {
            LengthModel *lenModel = general[i];
            name = lenModel.length;
        }
        else
        {
            name = general[i];
        }
        
            
        
        UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(20*displaywidth+(W+10*displaywidth)*(i%4), Height+45*displaywidth*(i/4), W, 30*displaywidth) ImageName:nil Target:self Action:@selector(closeTruckAction:) Title:name Font:16.0];
        btn.layer.cornerRadius = 5;
        btn.layer.masksToBounds = YES;
        UIColor *backgroungColors = RGB(232, 232, 232);
        UIColor *titColor = RGB(102, 102, 102);
        if (i == [typeIdAry[indexPath.section] integerValue])
        {
            backgroungColors = ThemeColor;
            titColor = [UIColor whiteColor];
        }
        [btn setTitleColor:titColor forState:UIControlStateNormal];
        btn.backgroundColor = backgroungColors;
        btn.tag = (indexPath.section+1)*1000+i;
        [cell.contentView addSubview:btn];
    }
    NSInteger line = num/4;
    if (num %4 != 0)
    {
        line+=1;
    }
    CGFloat H = Height+5*displaywidth+line*45*displaywidth;
    UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, H-1*displaywidth, SCREEN_WIDTH-30*displaywidth, 1*displaywidth)];
    lineAview.backgroundColor = RGB(238, 238, 238);
    [cell.contentView addSubview:lineAview];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *general;
    if (indexPath.section == 0)
    {
        return 0;
    }
    else if (indexPath.section == 1)
    {
        general = _allTypeAry;
        
    }
    else if (indexPath.section == 2)
    {
        if (_TruckType != 0)
        {
            TruckTypeModel *model = _allTypeAry[_TruckType];
            general = model.truckLengths;
        }
    }
    else
    {
        general = @[@"有尾板",@"无尾板"];
    }
    NSInteger num = general.count;
    CGFloat Height = 50*displaywidth;
    if (indexPath.section == 0)
    {
        Height = 15*displaywidth;
    }
    NSInteger line = num/4;
    if (num %4 != 0)
    {
        line+=1;
    }
    CGFloat H = Height + 5*displaywidth+line*45*displaywidth;
    return H;
}

- (void)closeTruckAction:(UIButton *)btn
{
    NSInteger index = btn.tag%1000;
    NSInteger section = btn.tag/1000-1;
    NSArray *typeIdAry = @[@"",s_Integer(_TruckType),s_Integer(_lenthType),s_Integer(_baffType)];
    if (index != [typeIdAry[section] integerValue])
    {
        if (section == 0)
        {
            
        }
        else if (section == 1)
        {
            _TruckType = index;
            _lenthType = 0;
        }
        else if (section == 2)
        {
            _lenthType = index;
        }
        else
        {
            _baffType = index;
        }
        [_tableview reloadData];
    }
    
    
    
}


- (void)requestTruckType
{
    WEAKSELF;
    [HYBaseHandler requestWithTheFormUrl:SERVER_truckType Parmers:nil Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
             _allTypeAry = [TruckTypeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
             dispatch_async(dispatch_get_main_queue(), ^{
                 
                 [weakSelf.tableview reloadData];
             });
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         
     }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
